﻿using System; 
using System.Text;
using System.Data.SqlClient;
using System.Collections.Generic; 
using System.Data;
using RBS.DBUtility;
using RBS.Common;
namespace RBS.Core.DAL  
{
	 	//加菜字段。
   
		public partial class D_Order_Details
	{
   		     
		public bool Exists(int Id)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) from D_Order_Details");
			strSql.Append(" where ");
			                                       strSql.Append(" Id = @Id  ");
                            			SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4)
			};
			parameters[0].Value = Id;

			return DbHelperSQL.Exists(strSql.ToString(),parameters);
		}
        public bool ExistsOrderId(int orderId,int uid)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from D_Order_Details");
            strSql.Append(" where ");
            strSql.Append(" OrderId = @OrderId  and UId=@uid");
            SqlParameter[] parameters = {
					new SqlParameter("@OrderId", SqlDbType.Int,4),
                    new SqlParameter("@uid", SqlDbType.Int,4)
			};
            parameters[0].Value = orderId;
            parameters[1].Value = uid; 
            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }
				
		
		/// <summary>
		/// 增加一条数据
		/// </summary>
		public int Add(RBS.Core.Model.D_Order_Details model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("insert into D_Order_Details(");			
            strSql.Append("OrderId,HCId,CName,Num,Price,DisAmount,IsGuest,UId,State");
			strSql.Append(") values (");
            strSql.Append("@OrderId,@HCId,@CName,@Num,@Price,@DisAmount,@IsGuest,@UId,@State");            
            strSql.Append(") ");            
            strSql.Append(";select @@IDENTITY");		
			SqlParameter[] parameters = {
			            new SqlParameter("@OrderId", SqlDbType.Int,4) ,            
                        new SqlParameter("@HCId", SqlDbType.Int,4) ,            
                        new SqlParameter("@CName", SqlDbType.NVarChar,50) ,            
                        new SqlParameter("@Num", SqlDbType.Int,4) ,            
                        new SqlParameter("@Price", SqlDbType.Decimal,9) ,            
                        new SqlParameter("@DisAmount", SqlDbType.Decimal,9) ,            
                        new SqlParameter("@IsGuest", SqlDbType.Int,4) ,            
                        new SqlParameter("@UId", SqlDbType.Int,4) ,            
                        new SqlParameter("@State", SqlDbType.Int,4)             
              
            };
			            
            parameters[0].Value = model.OrderId;                        
            parameters[1].Value = model.HCId;                        
            parameters[2].Value = model.CName;                        
            parameters[3].Value = model.Num;                        
            parameters[4].Value = model.Price;                        
            parameters[5].Value = model.DisAmount;                        
            parameters[6].Value = model.IsGuest;                        
            parameters[7].Value = model.UId;                        
            parameters[8].Value = model.State;                        
			   
			object obj = DbHelperSQL.GetSingle(strSql.ToString(),parameters);			
			if (obj == null)
			{
				return 0;
			}
			else
			{
				                    
            	return Convert.ToInt32(obj);
                                                                  
			}			   
            			
		}
		
		
		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(RBS.Core.Model.D_Order_Details model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("update D_Order_Details set ");
			                                                
            strSql.Append(" OrderId = @OrderId , ");                                    
            strSql.Append(" HCId = @HCId , ");                                    
            strSql.Append(" CName = @CName , ");                                    
            strSql.Append(" Num = @Num , ");                                    
            strSql.Append(" Price = @Price , ");                                    
            strSql.Append(" DisAmount = @DisAmount , ");                                    
            strSql.Append(" IsGuest = @IsGuest , ");                                    
            strSql.Append(" UId = @UId , ");                                    
            strSql.Append(" State = @State  ");            			
			strSql.Append(" where Id=@Id ");
						
SqlParameter[] parameters = {
			            new SqlParameter("@Id", SqlDbType.Int,4) ,            
                        new SqlParameter("@OrderId", SqlDbType.Int,4) ,            
                        new SqlParameter("@HCId", SqlDbType.Int,4) ,            
                        new SqlParameter("@CName", SqlDbType.NVarChar,50) ,            
                        new SqlParameter("@Num", SqlDbType.Int,4) ,            
                        new SqlParameter("@Price", SqlDbType.Decimal,9) ,            
                        new SqlParameter("@DisAmount", SqlDbType.Decimal,9) ,            
                        new SqlParameter("@IsGuest", SqlDbType.Int,4) ,            
                        new SqlParameter("@UId", SqlDbType.Int,4) ,            
                        new SqlParameter("@State", SqlDbType.Int,4)             
              
            };
						            
            parameters[0].Value = model.Id;                        
            parameters[1].Value = model.OrderId;                        
            parameters[2].Value = model.HCId;                        
            parameters[3].Value = model.CName;                        
            parameters[4].Value = model.Num;                        
            parameters[5].Value = model.Price;                        
            parameters[6].Value = model.DisAmount;                        
            parameters[7].Value = model.IsGuest;                        
            parameters[8].Value = model.UId;                        
            parameters[9].Value = model.State;                        
            int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		
		
		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(int Id)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from D_Order_Details ");
			strSql.Append(" where Id=@Id");
						SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4)
			};
			parameters[0].Value = Id;


			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		
				/// <summary>
		/// 批量删除一批数据
		/// </summary>
		public bool DeleteList(string Idlist )
		{
            
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from D_Order_Details ");
			strSql.Append(" where ID in ("+Idlist + ")  ");
			int rows=DbHelperSQL.ExecuteSql(strSql.ToString());
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
				
		
		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public RBS.Core.Model.D_Order_Details GetModel(int Id)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select Id, OrderId, HCId, CName, Num, Price, DisAmount, IsGuest, UId, State  ");			
			strSql.Append("  from D_Order_Details ");
			strSql.Append(" where Id=@Id");
						SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4)
			};
			parameters[0].Value = Id;

			
			RBS.Core.Model.D_Order_Details model=new RBS.Core.Model.D_Order_Details();
			DataSet ds=DbHelperSQL.Query(strSql.ToString(),parameters);
			
			if(ds.Tables[0].Rows.Count>0)
			{
												if(ds.Tables[0].Rows[0]["Id"].ToString()!="")
				{
					model.Id=int.Parse(ds.Tables[0].Rows[0]["Id"].ToString());
				}
																																if(ds.Tables[0].Rows[0]["OrderId"].ToString()!="")
				{
					model.OrderId=int.Parse(ds.Tables[0].Rows[0]["OrderId"].ToString());
				}
																																if(ds.Tables[0].Rows[0]["HCId"].ToString()!="")
				{
					model.HCId=int.Parse(ds.Tables[0].Rows[0]["HCId"].ToString());
				}
																																				model.CName= ds.Tables[0].Rows[0]["CName"].ToString();
																												if(ds.Tables[0].Rows[0]["Num"].ToString()!="")
				{
					model.Num=int.Parse(ds.Tables[0].Rows[0]["Num"].ToString());
				}
																																if(ds.Tables[0].Rows[0]["Price"].ToString()!="")
				{
					model.Price=decimal.Parse(ds.Tables[0].Rows[0]["Price"].ToString());
				}
																																if(ds.Tables[0].Rows[0]["DisAmount"].ToString()!="")
				{
					model.DisAmount=decimal.Parse(ds.Tables[0].Rows[0]["DisAmount"].ToString());
				}
																																if(ds.Tables[0].Rows[0]["IsGuest"].ToString()!="")
				{
					model.IsGuest=int.Parse(ds.Tables[0].Rows[0]["IsGuest"].ToString());
				}
																																if(ds.Tables[0].Rows[0]["UId"].ToString()!="")
				{
					model.UId=int.Parse(ds.Tables[0].Rows[0]["UId"].ToString());
				}
																																if(ds.Tables[0].Rows[0]["State"].ToString()!="")
				{
					model.State=int.Parse(ds.Tables[0].Rows[0]["State"].ToString());
				}
																														
				return model;
			}
			else
			{
				return null;
			}
		}
		
		
		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select * ");
			strSql.Append(" FROM D_Order_Details ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			return DbHelperSQL.Query(strSql.ToString());
		}
		
		/// <summary>
		/// 获得前几行数据
		/// </summary>
		public DataSet GetList(int Top,string strWhere,string filedOrder)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select ");
			if(Top>0)
			{
				strSql.Append(" top "+Top.ToString());
			}
			strSql.Append(" * ");
			strSql.Append(" FROM D_Order_Details ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			strSql.Append(" order by " + filedOrder);
			return DbHelperSQL.Query(strSql.ToString());
		}  
		 #region  ExtensionMethod
        /// <summary>
        /// 获得查询分页数据
        /// </summary>
        public DataSet GetList(int pageSize, int pageIndex, string strWhere, string filedOrder, out int recordCount)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * FROM D_Order_Details ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where 1=1 " + strWhere);
            }
            recordCount = Convert.ToInt32(DbHelperSQL.GetSingle(PagingHelper.CreateCountingSql(strSql.ToString())));
            return DbHelperSQL.Query(PagingHelper.CreatePagingSql(recordCount, pageSize, pageIndex, strSql.ToString(), filedOrder));
        }
		#endregion  ExtensionMethod 
            /// <summary>
            /// 获取订单金额
            /// </summary>
            /// <param name="orderid"></param>
            /// <returns></returns>
		    public decimal GetOrderMoney(int orderid)
		    {
                const string strSql = " select ISNULL(sum((Num*weight*DisAmount)),0) as OrderMoney  from V_OrderDetails where OrderId=@orderid"; 
                SqlParameter[] parameters = {
					new SqlParameter("@orderid", SqlDbType.Int,4)
			        };
                parameters[0].Value = orderid; 
                DataTable dt = DbHelperSQL.Query(strSql, parameters).Tables[0];
                return Convert.ToDecimal(dt.Rows[0]["OrderMoney"]);
		    }

	}
}

